package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntitySysFunction;
import com.loongtech.bi.entity.system.EntitySysModel;
import com.loongtech.bi.manager.system.SysFunctionManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysModelManager;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能注册
 * 
 * @author xckuang
 * @date 2014-10-07
 */
@Controller("functionRegisterAction")
@Scope("view")
public class FunctionRegisterAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	
	@Autowired
	private SysModelManager sysModelManager;
	@Autowired
	private SysFunctionManager sysFunctionManager;
	@Autowired
	private SysLogManager sysLogManager;
	private EntitySysFunction function;
	private Map<Integer, EntitySysModel> modelMap;
	private Map<Integer, String> statusMap;
	private static final String MsgTagFail = "增加功能失败";
	private static final String MsgTagSucc = "增加功能成功";

	@Autowired
	public void init() {
		setFunction(new EntitySysFunction());
		modelMap = new HashMap<Integer, EntitySysModel>();
		for(EntitySysModel domain : sysModelManager.findAll()) {
			modelMap.put(domain.getId(), domain);
		}
		
		statusMap = sysFunctionManager.getStatusMap();
	}

	public void add() {
		FacesContext context = FacesContext.getCurrentInstance();

		// 判断参数
		String name = function.getName();
		if(null == name || name.isEmpty()) {
			context.addMessage(null, new FacesMessage(MsgTagFail, "功能名称为空!"));
			return;
		}
		// 判断模块
		if(null == sysModelManager.get(function.getModelId())) {
			context.addMessage(null, new FacesMessage(MsgTagFail, "所选模块不存在为空!"));
			return;
		}
		// 判断用户name是否已经被注册
		if(null != sysFunctionManager.getBy(EntitySysFunction.K.name, name)) {
			context.addMessage(null, new FacesMessage(MsgTagFail, "功能名称已注册!"));
			return;
		}
		// 注册模块
		try {
			sysFunctionManager.persist(function);
			context.addMessage(null, new FacesMessage(MsgTagSucc, "功能名称为" + name));
			sysLogManager.addLog(session.getUserName(), MsgTagSucc + name);
			setFunction(new EntitySysFunction());
			return;

		} catch(Exception ex) {
			ex.printStackTrace();
		    context.addMessage(null, new FacesMessage(MsgTagFail, "注册时发生异常!"));
			return;
		}
	}

	public EntitySysFunction getFunction() {
		return function;
	}

	public void setFunction(EntitySysFunction function) {
		this.function = function;
	}

	public Map<Integer, EntitySysModel> getModelMap() {
		return modelMap;
	}

	public void setModelMap(Map<Integer, EntitySysModel> modelMap) {
		this.modelMap = modelMap;
	}

	public Map<Integer, String> getStatusMap() {
		return statusMap;
	}

	public void setStatusMap(Map<Integer, String> statusMap) {
		this.statusMap = statusMap;
	}
}
